package com.etone.web.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.etone.common.annotation.Anonymous;
import com.etone.common.annotation.Log;
import com.etone.common.core.controller.BaseController;
import com.etone.common.core.domain.AjaxResult;
import com.etone.common.enums.BusinessType;
import com.etone.common.utils.DateUtils;
import com.etone.common.utils.http.HttpUtils;
import com.etone.system.domain.*;
import com.etone.system.mapper.FiveTaskinfoMapper;
import com.etone.system.mapper.SixFreetaskMapper;
import com.etone.system.mapper.SysDibiaoMapper;
import com.etone.system.service.*;
import com.etone.system.service.impl.AgvServiceImpl;
import com.etone.system.service.impl.TurnoverInventoryServiceImpl;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/system/agv")
public class AgvController extends BaseController {


    @Autowired
    private AgvService agvService;

    @Autowired
    private IFiveTaskinfoService fiveTaskinfoService;

    @Autowired
    private IFiveCtuRrecordService fiveCtuRrecordService;

    @Autowired
    private IFiveCtuRecordService fiveCtuRecordService;

    @Autowired
    private ISysDibiaoService sysDibiaoService;

    @Autowired
    private IFiveCtuCrecordService fiveCtuCrecordService;

    @Autowired
    private ISysStorageService sysStorageService;

    @Autowired
    private ISixWuliaoinfoService sixWuliaoinfoService;

    @Autowired
    private ISixFreetaskService sixFreetaskService;

    @Autowired
    private FiveTaskinfoMapper fiveTaskinfoMapper;

    @Autowired
    private SysDibiaoMapper dibiaoMapper;

    @Autowired
    private SixFreetaskMapper sixFreetaskMapper;

    private static String rcsUrl = "http://172.16.0.200:8182/rcms/services/rest/hikRpcService/";

    /**agv回调信息通知*/
    @Log(title = "任务执行通知", businessType = BusinessType.INSERT)
    @Anonymous
    @PostMapping("/agvCallback")
    public JSONObject agvCallback(@Validated @RequestBody String param) {
        JSONObject jsonparam = JSONObject.parseObject(param);
        String taskType = "";
                //jsonparam.get("taskTyp").toString();
        String method = jsonparam.get("method").toString();
        if(jsonparam.get("method").toString().contains("cancel")){
            if(taskType.equals("Q1")){

            }else if(taskType.equals("Q2")){

            }else if(taskType.equals("Q3")){

            }else if(taskType.equals("Q4")){

            }else if(taskType.equals("Q5")){

            }else if(taskType.equals("Q6")){

            }else if(taskType.equals("T1")){

            }else if(taskType.equals("T2")){

            }
        }
        if(method.equals("end1")){
            taskType = "Q1";
        }else if(method.equals("end2")){
            taskType = "Q2";
        }else if(method.equals("end3")){
            taskType = "Q3";
        }else if(method.equals("end4")){
            taskType = "Q4";
        }else if(method.equals("end5")){
            taskType = "Q5";
        }else if(method.equals("end6")){
            taskType = "Q6";
        }else if(method.equals("end7")){
            taskType = "T1";
        }else if(method.equals("end8")){
            taskType = "T2";
        }
//        if(jsonparam.get("method").toString().equals("end")) {
            if(taskType.equals("Q1")){//此Q1代表背货架入库成功后回调方法 入库位到入库周转区
                //普通背货架agv回调成功方法,说明有有货货架放在入库周转区了，就要判断是否要主动调用起CTU小车
                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("currentPositionCode").toString();
                //先考虑第一次的情况
                //获取数据库中入库成功的仓位，按创建时间的早晚的依次获取，以符合6的倍数的数字发送
                //首先判断ETU货架是否有空闲位置
                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("入库");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                String taskCode = jsonparam.get("taskCode").toString();
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);

                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                String[] remark1 = list.get(0).getRemark().split("---");
                String remark  = remark1[0];
                String a  = remark1[1];
                String atr = a;
                Gson gson = new Gson();
                List<TurnoverInventory> list1 = Arrays.asList(gson.fromJson(remark, TurnoverInventory[].class));
                TurnoverInventory turnoverInventory = null;
                String huojia = "";
                if(list1.size()>0){
                    turnoverInventory = list1.get(0);
                    huojia = turnoverInventory.getCangweiNo().substring(0,6);
                }
                if(a.equals("1")){
                    a = "0";
                }
                binddibiaoinfo(wbCode,currentPositionCode,huojia,a);

                String type = "0";//0-入库；1-出库
                //需要判断CTU货架是否已满，如果库存满了的话，则暂时记录在等待任务表中,有空闲时则触发
//                SysStorage sysStorage = new SysStorage();
//                sysStorage.setIsFree(0l);
//                sysStorage.setIsRequest(0l);
//                List<Map<String,Object>> storages = sysStorageService.selectSysStorageList(sysStorage);
//                if(storages.size()!=0){
                    agvService.callRCTUAgv(type,taskCode,list1,huojia,atr);
//                }else{
//                    //说明CTU库存已经满了,将后续入库任务记录下来
//                    SixFreetask sixFreetask = new SixFreetask();
//                    sixFreetask.setType("0");
//                    sixFreetask.setTaskModeltype(taskCode);
//                    sixFreetask.setWuliaoInfo(list.get(0).getRemark());
////                sixFreetask.setStartp();
////                sixFreetask.setEndp();
////                sixFreetask.setHuojia();
////                sixFreetask.setStartText();
////                sixFreetask.setEndText();
//                    sixFreetaskMapper.insertSixFreetask(sixFreetask);
//                }

                //实现货架旧地标的解绑，新地标的绑定
                String response = AgvServiceImpl.bangding(huojia,wbCode,0);
                String response1 = AgvServiceImpl.bangding(huojia,currentPositionCode,1);
                //及时将入库周转区的一个空货架搬运到入库位或者出库位
                String freehuojia = gainFreeHuojia(2l);
                String taskModelType = "Q2";
                Long startP = 2l;
                Long endP = null;
                String text = "";
                String endText = "";
                if(atr.equals("1")){
                    endP = 1l;
                    text = "入库周转区---> 入库位";
                    endText = "普通背货架agv执行空货架搬运任务---";
                }else if(atr.equals("4")){
                    endP = 4l;
                    text = "入库周转区---> 出库位";
                    endText = "普通背货架agv执行空货架搬运任务---";
                }
                if(!freehuojia.equals("")){
                    operateFreeHuojia(taskModelType,startP,endP,freehuojia,text,endText,
                            sysDibiaoService,fiveTaskinfoService,sixFreetaskService);
                }else{
                    //记录入库周转区到入库位的空货架搬运任务
                    SixFreetask sixFreetask = new SixFreetask();
                    sixFreetask.setType("4");
                    sixFreetask.setTaskModeltype(taskModelType);
//                sixFreetask.setWuliaoInfo(json1.toString());
                    sixFreetask.setStartp(startP.toString());
                    sixFreetask.setEndp(endP.toString());
//                        sixFreetask.setHuojia(huojia);
                    sixFreetask.setStartText(text);
                    sixFreetask.setEndText(endText);
                    sixFreetaskService.insertSixFreetask(sixFreetask);
                }
            }else if(taskType.equals("Q2")){//潜伏空货架从入库周转到显影区
                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("currentPositionCode").toString();
                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("入库周转区---> 入库位");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                String taskCode = jsonparam.get("taskCode").toString();
                fiveTaskinfo.setTaskCode(taskCode);

                //重新建立空货架的地标绑定关系
                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                String[] remarkarr = list.get(0).getRemark().split("---");
                String remark = remarkarr[0];
                String a = remarkarr[1];
                String yunxinginfo = "";
                if(a.equals("4")){
                    a = "4";
                    yunxinginfo = "入库周转区---> 出库位";
                }else if(a.equals("1")){
                    a = "0";
                    yunxinginfo = "入库周转区---> 入库位";
                }
                binddibiaoinfo(wbCode,currentPositionCode,remark,a);

                fiveTaskinfo.setTaskType(yunxinginfo);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);

                //对货架进行绑定解绑
                String response = AgvServiceImpl.bangding(remark,wbCode,0);
                String response1 = AgvServiceImpl.bangding(remark,currentPositionCode,1);
            }else if(taskType.equals("Q3")){//加急 潜伏背货架从显影区到出库位
                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("currentPositionCode").toString();
                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("加急潜伏---入库位直接到出库位");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                String taskCode = jsonparam.get("taskCode").toString();
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);

                //重新建立空货架的地标绑定关系
                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                String[] remark1 = list.get(0).getRemark().split("---");
                String remark  = remark1[0];
                Gson gson = new Gson();
                List<TurnoverInventory> list1 = Arrays.asList(gson.fromJson(remark, TurnoverInventory[].class));
                TurnoverInventory turnoverInventory = null;
                String huojia = "";
                if(list1.size()>0){
                    turnoverInventory = list1.get(0);
                    huojia = turnoverInventory.getCangweiNo().substring(0,6);
                }
                binddibiaoinfo(wbCode,currentPositionCode,huojia,"4");

                SysDibiao sysDibiao = new SysDibiao();
                sysDibiao.setHuojia(huojia);
                List<SysDibiao> dblist = sysDibiaoService.selectSysDibiaoList(sysDibiao);
                sysDibiao = dblist.get(0);
                sysDibiao.setMark("0");
                sysDibiaoService.updateSysDibiao(sysDibiao);

                //将来自入库位的货架重新绑定解绑
                String response = AgvServiceImpl.bangding(huojia,wbCode,0);
                String response1 = AgvServiceImpl.bangding(huojia,currentPositionCode,1);
                String freehuojia = gainFreeHuojia(4l);
                String taskModelType = "Q4";
                Long startP = 4l;
                Long endP = 1l;
                String text = "加急潜伏---出库位直接到入库位";
                String endText = "普通背货架agv执行空货架搬运任务---";
                if(!freehuojia.equals("")){
                    AgvServiceImpl.operateFreeHuojia(taskModelType,startP,endP,freehuojia,text,endText,
                            dibiaoMapper,fiveTaskinfoMapper,sixFreetaskMapper);
                }else{
                    //记录入库周转区到入库位的空货架搬运任务
                    SixFreetask sixFreetask = new SixFreetask();
                    sixFreetask.setType("4");
                    sixFreetask.setTaskModeltype(taskModelType);
//                sixFreetask.setWuliaoInfo(json1.toString());
                    sixFreetask.setStartp(startP.toString());
                    sixFreetask.setEndp(endP.toString());
//                        sixFreetask.setHuojia(huojia);
                    sixFreetask.setStartText(text);
                    sixFreetask.setEndText(endText);
                    sixFreetaskService.insertSixFreetask(sixFreetask);
                }
            }else if(taskType.equals("Q4")){//加急 潜伏背空货架从出库位到显影区
                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("currentPositionCode").toString();
                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("加急潜伏---出库位直接到入库位");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                String taskCode = jsonparam.get("taskCode").toString();
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);


                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                String remark = list.get(0).getRemark().split("---")[0];

                SysDibiao sysDibiao = new SysDibiao();
                sysDibiao.setHuojia(remark);
                List<SysDibiao> dblist = sysDibiaoService.selectSysDibiaoList(sysDibiao);
                sysDibiao = dblist.get(0);
                sysDibiao.setMark("0");
                sysDibiaoService.updateSysDibiao(sysDibiao);

                binddibiaoinfo(wbCode,currentPositionCode,remark,"4");

                String response = AgvServiceImpl.bangding(remark,wbCode,0);
                String response1 = AgvServiceImpl.bangding(remark,currentPositionCode,1);
            }else if(taskType.equals("Q5")){//潜伏从出库周转到出库位
                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("currentPositionCode").toString();
                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("出库周转区---> 出库位");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                String taskCode = jsonparam.get("taskCode").toString();
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);

                //重新建立满货架的地标绑定关系
                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                String[] remarkarr = list.get(0).getRemark().split("---");
                String remark = remarkarr[0];
                String a = remarkarr[1];
//                String remark = list.get(0).getRemark();
                binddibiaoinfo(wbCode,currentPositionCode,remark,"0");

                String response = AgvServiceImpl.bangding(remark,wbCode,0);
                String response1 = AgvServiceImpl.bangding(remark,currentPositionCode,1);

                //记录从出库周转区到出库位的货架记录，并记录开始时间与要结束时间也就是待清空时间
                SixFreetask sixFreetask1 = new SixFreetask();
                sixFreetask1.setType("5");
//                sixFreetask.setTaskModeltype(taskModelType);
//                sixFreetask.setWuliaoInfo(json1.toString());
//                sixFreetask.setStartp(startP.toString());
//                sixFreetask.setEndp(endP.toString());
                sixFreetask1.setHuojia(remark);
//                sixFreetask.setStartText(text);
//                sixFreetask.setEndText(endText);
                sixFreetaskService.insertSixFreetask(sixFreetask1);

                //同时要将出库位上的空货架搬运到出库周转区
                String freehuojia = gainFreeHuojia(4l);
                String taskModelType = "Q6";
                Long startP = 4l;
                Long endP = 3l;
                String text = "出库位---> 出库周转区";
                String endText = "普通背货架agv执行空货架搬运任务---";
                if(!freehuojia.equals("")){
                    AgvServiceImpl.operateFreeHuojia(taskModelType,startP,endP,freehuojia,text,endText,
                            dibiaoMapper,fiveTaskinfoMapper,sixFreetaskMapper);
                }else{
                    //记录入库周转区到入库位的空货架搬运任务
                    SixFreetask sixFreetask = new SixFreetask();
                    sixFreetask.setType("4");
                    sixFreetask.setTaskModeltype(taskModelType);
//                sixFreetask.setWuliaoInfo(json1.toString());
                    sixFreetask.setStartp(startP.toString());
                    sixFreetask.setEndp(endP.toString());
//                        sixFreetask.setHuojia(huojia);
                    sixFreetask.setStartText(text);
                    sixFreetask.setEndText(endText);
                    sixFreetaskService.insertSixFreetask(sixFreetask);
                }
            }else if(taskType.equals("Q6")){//潜伏从出库位到出库周转
                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("currentPositionCode").toString();
                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("出库位---> 出库周转区");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                String taskCode = jsonparam.get("taskCode").toString();
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);

                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                String remark = list.get(0).getRemark().split("---")[0];
                binddibiaoinfo(wbCode,currentPositionCode,remark,"0");

                String response = AgvServiceImpl.bangding(remark,wbCode,0);
                String response1 = AgvServiceImpl.bangding(remark,currentPositionCode,1);
            }else if(taskType.equals("T1")){//CTU 入库 从周转货架仓位到CTU货架仓位
//                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("dstBinCode").toString();
                String taskCode = jsonparam.get("taskCode").toString();

                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                FiveTaskinfo fiveTaskinfo2 = list.get(0);
                String remark = fiveTaskinfo2.getRemark();
                String wbCode = fiveTaskinfo2.getTaskStartPosition();
                String huojiatongyiCode = fiveTaskinfo2.getHuojiatongyiCode();
                String[] remarkInfo = remark.split("---");
                String wuliaono = remarkInfo[0];
                String[] startInfo = remarkInfo[1].split("/");
                String[] endInfo = remarkInfo[2].split("/");
                String[] numInfo = remarkInfo[3].split("/");
                String[] aInfo = remarkInfo[4].split("/");

                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("CTU入库");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfo.setHuojiatongyiCode(huojiatongyiCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);
                //暂不记录执行普通货架入库的出库记录
                //执行CTU货架货物的入库记录
                FiveCtuRrecord fiveCtuRrecord = new FiveCtuRrecord();
                fiveCtuRrecord.setWuliaoCode(wuliaono);
                fiveCtuRrecord.setStartRegionId(Long.parseLong(startInfo[0]));
                fiveCtuRrecord.setStartArrangeId(Long.parseLong(startInfo[1]));
                fiveCtuRrecord.setStartLieId(Long.parseLong(startInfo[2]));
                fiveCtuRrecord.setStartStorageId(Long.parseLong(startInfo[3]));

                String regionId = endInfo[0];
                String arrangeId = endInfo[1];
                String lieId = endInfo[2];
                String storageId = endInfo[3];
                fiveCtuRrecord.setEndRegionId(Long.parseLong(regionId));
                fiveCtuRrecord.setEndArrangeId(Long.parseLong(arrangeId));
                fiveCtuRrecord.setEndLieId(Long.parseLong(lieId));
                fiveCtuRrecord.setEndStorageId(Long.parseLong(storageId));
                fiveCtuRrecord.setStatus("入库完成");
                fiveCtuRrecordService.insertFiveCtuRrecord(fiveCtuRrecord);

                //新增物料溯源
                Map<String,Object> storageMap = sysStorageService.selectSysStorageById(Long.parseLong(storageId));
                sixWuliaoinfoService.deleteSixWuliaoinfoBywuliaocode(wuliaono);
                SixWuliaoinfo sixWuliaoinfo = new SixWuliaoinfo();
                String cangweiNo = storageMap.get("cangweiNo").toString();
                sixWuliaoinfo.setPaiCode(cangweiNo.substring(0,2));
                sixWuliaoinfo.setHuojiaCode(cangweiNo.substring(0,6));
                sixWuliaoinfo.setStorageCode(cangweiNo);
                sixWuliaoinfo.setWuliaoCode(wuliaono);
                sixWuliaoinfoService.insertSixWuliaoinfo(sixWuliaoinfo);

                //添加入库记录的同时还要添加库存信息
                FiveCtuRecord fiveCtuRecord = new FiveCtuRecord();
                fiveCtuRecord.setWuliaoCode(wuliaono);
                fiveCtuRecord.setRegionId(Long.parseLong(regionId));
                fiveCtuRecord.setArrangeId(Long.parseLong(arrangeId));
                fiveCtuRecord.setLieId(Long.parseLong(lieId));
                fiveCtuRecord.setStorageId(Long.parseLong(storageId));
                fiveCtuRecordService.insertFiveCtuRecord(fiveCtuRecord);

                //修改仓位绑定关系
                bindcangweiornobind(wbCode,0l,0l);
                bindcangweiornobind(currentPositionCode,1l,1l);

//                String num = numInfo[0];
//                String size = numInfo[1];
//                FiveTaskinfo fiveTaskinfo3 = new FiveTaskinfo();
//                fiveTaskinfo3.setHuojiatongyiCode(huojiatongyiCode);
//                fiveTaskinfo3.setStatus("待完成");
//                List<FiveTaskinfo> list3 = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo3);
//
//                FiveTaskinfo fiveTaskinfo4 = new FiveTaskinfo();
//                fiveTaskinfo4.setHuojiatongyiCode(huojiatongyiCode);
//                fiveTaskinfo4.setStatus("完成");
//                List<FiveTaskinfo> list4 = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo4);
//                if(list3.size() == list4.size()){
//                    //说明周转货架上的货物都已被CTu货架取走，执行周转空货架搬运任务
//                    String a = aInfo[0];
//                    String huojia = aInfo[1];
//                    Long al = null;
//                            //Long.parseLong(a);
//                    String text = "";
//                    if(a.equals("1")){
//                        al = 1l;
//                        text = "入库周转区---> 入库位";
//                    }else
//                        if(a.equals("4")){
//                        //还要区分是加急潜伏过来的还是正常过来的
//                        //判断货架号关联地标的属性
//                        SysDibiao sysDibiao = new SysDibiao();
//                        sysDibiao.setHuojia(huojia);
//                        List<SysDibiao> dibiaoinfos = sysDibiaoService.selectSysDibiaoList(sysDibiao);
//                        String mark = dibiaoinfos.get(0).getMark();
//                        if(mark.equals("0")){
//                            al = 3l;
//                            text = "入库周转区---> 出库周转区";
//                        }else if(mark.equals("1")){
//                            al = 1l;
//                            text = "入库周转区---> 入库位";
//                        }
//                    }
//                    operateFreeHuojia("Q2",2l,al,huojia,text,"普通背货架agv执行空货架搬运任务---",
//                            sysDibiaoService,fiveTaskinfoService,sixFreetaskService);
//                }
            }else if(taskType.equals("T2")){//CTU 出库 从CTU货架仓位到周转货架仓位
//                String wbCode = jsonparam.get("wbCode").toString();
                String currentPositionCode = jsonparam.get("dstBinCode").toString();
                String taskCode = jsonparam.get("taskCode").toString();

                FiveTaskinfo fiveTaskinfo1 = new FiveTaskinfo();
                fiveTaskinfo1.setTaskCode(taskCode);
                fiveTaskinfo1.setStatus("待完成");
                List<FiveTaskinfo> list = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo1);
                FiveTaskinfo fiveTaskinfo2 = list.get(0);
                String remark = fiveTaskinfo2.getRemark();
                String wbCode = fiveTaskinfo2.getTaskStartPosition();
                String huojiatongyiCode = fiveTaskinfo2.getHuojiatongyiCode();
                String[] remarkInfo = remark.split("---");
                String wuliaono = remarkInfo[0];
                String[] startInfo = remarkInfo[1].split("/");
                String[] endInfo = remarkInfo[2].split("/");
                String[] numInfo = remarkInfo[3].split("/");
                String huojia = remarkInfo[4];

                FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
                fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskStartPosition(wbCode);
                fiveTaskinfo.setTaskEndPosition(currentPositionCode);
                fiveTaskinfo.setTaskType("CTU出库");
                fiveTaskinfo.setTaskStartInfo("");
                fiveTaskinfo.setTaskEndInfo("");
                fiveTaskinfo.setStatus("完成");
                fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
                fiveTaskinfo.setTaskCode(taskCode);
                fiveTaskinfo.setHuojiatongyiCode(huojiatongyiCode);
                fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);
                //暂不记录执行普通货架入库的出库记录
                //执行CTU货架货物的出库记录
                FiveCtuCrecord fiveCtuCrecord = new FiveCtuCrecord();
                fiveCtuCrecord.setWuliaoCode(wuliaono);
                fiveCtuCrecord.setStartRegionId(Long.parseLong(startInfo[0]));
                fiveCtuCrecord.setStartArrangeId(Long.parseLong(startInfo[1]));
                fiveCtuCrecord.setStartLieId(Long.parseLong(startInfo[2]));
                fiveCtuCrecord.setStartStorageId(Long.parseLong(startInfo[3]));

                String regionId = endInfo[0];
                String arrangeId = endInfo[1];
                String lieId = endInfo[2];
                String storageId = endInfo[3];
                fiveCtuCrecord.setEndRegionId(Long.parseLong(regionId));
                fiveCtuCrecord.setEndArrangeId(Long.parseLong(arrangeId));
                fiveCtuCrecord.setEndLieId(Long.parseLong(lieId));
                fiveCtuCrecord.setEndStorageId(Long.parseLong(storageId));
                fiveCtuCrecord.setStatus("出库完成");
                fiveCtuCrecordService.insertFiveCtuCrecord(fiveCtuCrecord);

                //新增物料溯源
                Map<String,Object> storageMap = sysStorageService.selectSysStorageById(Long.parseLong(storageId));
                sixWuliaoinfoService.deleteSixWuliaoinfoBywuliaocode(wuliaono);
                SixWuliaoinfo sixWuliaoinfo = new SixWuliaoinfo();
                String cangweiNo = storageMap.get("cangweiNo").toString();
                sixWuliaoinfo.setPaiCode(cangweiNo.substring(0,2));
                sixWuliaoinfo.setHuojiaCode(cangweiNo.substring(0,6));
                sixWuliaoinfo.setStorageCode(cangweiNo);
                sixWuliaoinfo.setWuliaoCode(wuliaono);
                sixWuliaoinfoService.insertSixWuliaoinfo(sixWuliaoinfo);


                //添加出库记录的同时还要删除对应库存信息
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("storageId",startInfo[3]);
                map.put("wuliaocode",wuliaono);
                fiveCtuRecordService.deleteFiveCtuRecordByStorageIdAndwuliaoInfo(map);

                //修改仓位绑定关系
                bindcangweiornobind(wbCode,0l,0l);
                bindcangweiornobind(currentPositionCode,1l,1l);

//                String num = numInfo[0];
//                String size = numInfo[1];
                FiveTaskinfo fiveTaskinfo3 = new FiveTaskinfo();
                fiveTaskinfo3.setHuojiatongyiCode(huojiatongyiCode);
                fiveTaskinfo3.setStatus("待完成");
                List<FiveTaskinfo> list3 = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo3);

                FiveTaskinfo fiveTaskinfo4 = new FiveTaskinfo();
                fiveTaskinfo4.setHuojiatongyiCode(huojiatongyiCode);
                fiveTaskinfo4.setStatus("完成");
                List<FiveTaskinfo> list4 = fiveTaskinfoService.selectFiveTaskinfoList(fiveTaskinfo4);
                if(list3.size() == list4.size()){
                    Map<String,Object> map2 = operateFreeHuojia("Q5",3l,4l,huojia,
                            "出库周转区---> 出库位","普通背货架agv执行满货架搬运任务---",sysDibiaoService,fiveTaskinfoService,sixFreetaskService);
                }
            }
//        }
        JSONObject json = JSONObject.parseObject(param);
        String result = "{\n" +
                "\"code\": \"0\",\n" +
                "\"message\": \"成功\",\n" +
                "\"reqCode\": \"" + json.get("reqCode").toString() + "\" }";
        JSONObject jsonRes = JSONObject.parseObject(result);
        return jsonRes;
    }
    /**
     * 地标动态绑定
     * */
    public void binddibiaoinfo(String wbCode,String currentPositionCode,String huojia,String a){
        //修改起始地标关联关系
        SysDibiao sysDibiao = new SysDibiao();
        sysDibiao.setName(wbCode);
        List<SysDibiao> dblist = sysDibiaoService.selectSysDibiaoList(sysDibiao);
        String mark = "";
        if(dblist.size()!=0){
            sysDibiao = dblist.get(0);
            mark = sysDibiao.getMark();
            sysDibiao.setHuojia(null);
            sysDibiao.setIsFree("0");
            sysDibiao.setMark("0");
            sysDibiaoService.updateSysDibiao(sysDibiao);
        }
        //修改结束地标关联关系
        sysDibiao = new SysDibiao();
        sysDibiao.setName(currentPositionCode);
        List<SysDibiao> dblists = sysDibiaoService.selectSysDibiaoList(sysDibiao);
        if(dblists.size()!=0){
            sysDibiao = dblists.get(0);
            sysDibiao.setHuojia(huojia);
            if(a.equals("4")){
                sysDibiao.setMark(mark);
            }
            sysDibiaoService.updateSysDibiao(sysDibiao);
        }
    }
    public void bindcangweiornobind(String position,long isrequest,long isfree){
        SysStorage sysStorage = new SysStorage();
        sysStorage.setCangweiNo(position);
        List<Map<String,Object>> list = sysStorageService.selectSysStorageList(sysStorage);
        Map<String,Object> map5 = list.get(0);
        sysStorage.setCangweiNo(null);
        sysStorage.setId(Long.parseLong(map5.get("id").toString()));
        sysStorage.setIsRequest(isrequest);
        sysStorage.setIsFree(isfree);
        sysStorageService.updateSysStorage(sysStorage);
    }
    @Anonymous
    @GetMapping("/createTask")
    public AjaxResult createTask(String taskModelType,String startPosition,String endPosition){
        String response = "";
        String param = createParamInfo(taskModelType,startPosition,endPosition);
        JSONObject json = JSONObject.parseObject(param);
        String reqCode = UUID.randomUUID().toString().substring(0, 20);
        json.fluentPut("reqCode", reqCode);
        json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        json.fluentPut("taskCode", reqCode);
        response = HttpUtils.sendRestPost(rcsUrl+"genAgvSchedulingTask",json);
        return AjaxResult.success(response);
    }
    public static String createParamInfo(String taskModelType,String startPosition,String endPosition){
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"taskTyp\": \""+taskModelType+"\",\n" +
                "    \"ctnrTyp\": \"\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"positionCodePath\": [\n" +
                "        {\n" +
                "            \"positionCode\": \""+startPosition+"\",\n" +
                "            \"type\": \"00\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"positionCode\": \""+endPosition+"\",\n" +
                "            \"type\": \"00\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
//                "    \"priority\": \"6\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        return  param;
    }
    /**
     * agv执行空货架搬运任务
     * */
    public static Map<String,Object> operateFreeHuojia(String taskModelType, Long startP, Long endP, String huojia, String taskType,
                                                       String taskEndInfo, ISysDibiaoService sysDibiaoService ,
                                                       IFiveTaskinfoService fiveTaskinfoService,ISixFreetaskService sixFreetaskService){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            //获取开始位置与技术位置
            Map<String,Object> map1 = gainweizhiInfo(huojia,startP,endP,sysDibiaoService);
            String startPosition = map1.get("startPosition").toString();
            String endPosition = map1.get("endPosition").toString();

            if(endPosition.equals("")){
                //需要将此操作记录在流程中，等到有空闲货架出现执行搬运任务
                SixFreetask sixFreetask = new SixFreetask();
                sixFreetask.setType("3");
                sixFreetask.setTaskModeltype(taskModelType);
//                sixFreetask.setWuliaoInfo(json1.toString());
                sixFreetask.setStartp(startP.toString());
                sixFreetask.setEndp(endP.toString());
                sixFreetask.setHuojia(huojia);
                sixFreetask.setStartText(taskType);
                sixFreetask.setEndText(taskEndInfo);
                sixFreetaskService.insertSixFreetask(sixFreetask);
                map.put("code",100);
                map.put("msg","未查询到有效终点位置！");
                return map;
            }

            //说明货架上货物已被清空
            FiveTaskinfo fiveTaskinfo = new FiveTaskinfo();
            fiveTaskinfo.setTaskStartTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setTaskStartPosition(startPosition);
            fiveTaskinfo.setTaskEndPosition(endPosition);
            fiveTaskinfo.setTaskType(taskType);

            String param = createParamInfo(taskModelType,startPosition,endPosition);
            JSONObject json = JSONObject.parseObject(param);
            String reqCode = UUID.randomUUID().toString().substring(0, 20);
            json.fluentPut("reqCode", reqCode);
            json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            json.fluentPut("taskCode", reqCode);

            fiveTaskinfo.setTaskCode(reqCode);
            fiveTaskinfo.setTaskStartInfo(json.toString());
            String response = "";
            response = HttpUtils.sendRestPost(rcsUrl+"genAgvSchedulingTask",json);
            fiveTaskinfo.setTaskEndInfo(taskEndInfo+response);
            fiveTaskinfo.setStatus("待完成");
            fiveTaskinfo.setTaskEndTime(DateUtils.dateTimeNow("YYYY-MM-dd HH:mm:ss,SSS"));
            fiveTaskinfo.setRemark(huojia+"---"+endP.toString());
            fiveTaskinfoService.insertFiveTaskinfo(fiveTaskinfo);
            map.put("code",200);
            map.put("msg","操作成功！");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",100);
            map.put("msg",e.getMessage());
        }
        return map;
    }
    public static Map<String ,Object> gainweizhiInfo(String huojiano,Long startP,Long endP,ISysDibiaoService sysDibiaoService){
        Map<String,Object> map = new HashMap<>();
        String startPosition= "";
        String endPosition = "";
        SysDibiao sysDibiao = new SysDibiao();
        sysDibiao.setType(startP);
        sysDibiao.setHuojia(huojiano);
        List<SysDibiao> dblist = sysDibiaoService.selectSysDibiaoList(sysDibiao);
        SysDibiao sysDibiao1 = dblist.get(0);
        startPosition = sysDibiao1.getName();
        String mark = sysDibiao1.getMark();
        sysDibiao =  new SysDibiao();
        sysDibiao.setType(endP);
        List<SysDibiao> dblists = sysDibiaoService.selectSysFreeDibiaoList(sysDibiao);
        if(dblists.size()!=0){
            sysDibiao = dblists.get(0);
            //对取得的结束位置预占用
            sysDibiao.setIsFree("1");
            String e = endP.toString();
            if(e.equals("3")){
                sysDibiao.setMark(mark);
            }
            sysDibiaoService.updateSysDibiao(sysDibiao);
            endPosition = sysDibiao.getName();
        }
        map.put("startPosition",startPosition);
        map.put("endPosition",endPosition);
        return map;
    }
    public String gainFreeHuojia(long type){
        String freehuojia = "";
        SysDibiao sysDibiao = new SysDibiao();
        sysDibiao.setType(type);
        List<SysDibiao> dibiaos = sysDibiaoService.selectSysDibiaoList(sysDibiao);
        for(SysDibiao sysDibiao1 : dibiaos){
            String huojia1 = sysDibiao1.getHuojia();
            if(huojia1!=null){
                //判断是否是空货架
                SixWuliaoinfo sixWuliaoinfo = new SixWuliaoinfo();
                sixWuliaoinfo.setHuojiaCode(huojia1);
                List<SixWuliaoinfo> list2 =
                        sixWuliaoinfoService.selectSixWuliaoinfoList(sixWuliaoinfo);
                if(list2.size()==0){
                    freehuojia = huojia1;
                    break;
                }
            }
        }
        return freehuojia;
    }
    /**
     * pda入库仓位号后端验证
     * */
    @Anonymous
    @GetMapping("/veriycangweiNo")
    public AjaxResult veriycangweiNo(String cangweiNo){
        SysStorage sysStorage = new SysStorage();
        sysStorage.setCangweiNo(cangweiNo);
        List<Map<String,Object>> maps = sysStorageService.selectSysStorageList(sysStorage);
        boolean flag = false;
        if(maps.size()!=0){
            flag = true;
        }
        return AjaxResult.success(flag);
    }
    /**
     * pda入库物料号后端验证
     * */
    @Anonymous
    @GetMapping("/veriyWuliaoCode")
    public AjaxResult veriyWuliaoCode(String wuliaoCode){
        //着重校验物料号为空的情况以及格式检验
        boolean flag = false;
        if(!wuliaoCode.equals("")){
            //对物料号格式校验：第一为为字母、中间有-、最大位数小于16
            int length = wuliaoCode.length();
            if(length <= 15){
                if(wuliaoCode.indexOf("-")!=-1){
                    String one = wuliaoCode.substring(0,1);
                    if (Character.isLetter(one.charAt(0))) {//true
                        SixWuliaoinfo sixWuliaoinfo = new SixWuliaoinfo();
                        sixWuliaoinfo.setWuliaoCode(wuliaoCode);
                        List<SixWuliaoinfo> list = sixWuliaoinfoService.selectSixWuliaoinfoList(sixWuliaoinfo);
                        if(list.size()==0){
                            flag = true;
                        }
                    }
                }
            }
        }else{
            flag = true;
        }
        return AjaxResult.success(flag);
    }
    /**
     * pda新增人工出库功能
     * */
    @Anonymous
    @PostMapping("/userOutStorage")
    public AjaxResult userOutStorage(@RequestBody List<TurnoverInventory> list){
        //对所有人工出库的物料进行一下处理
        // 添加人工出库记录
        //清库存记录，
        //清物料溯源记录
        //清空仓位
        //仓位解绑
        Map<String, Object> data = new HashMap<>();
        try{
            for(TurnoverInventory turnoverInventory : list){
                FiveCtuCrecord fiveCtuCrecord = new FiveCtuCrecord();
                String wuliaoCode = turnoverInventory.getWuliaoNo();
                fiveCtuCrecord.setWuliaoCode(wuliaoCode);
                //通过wuliaoCode获取原来存储位置
                FiveCtuRecord fiveCtuRecord = new FiveCtuRecord();
                fiveCtuRecord.setWuliaoCode(wuliaoCode);
                List<FiveCtuRecord> records = fiveCtuRecordService.selectFiveCtuRecordList(fiveCtuRecord);
                FiveCtuRecord fiveCtuRecord1 = records.get(0);
                fiveCtuCrecord.setStartRegionId(fiveCtuRecord1.getRegionId());
                fiveCtuCrecord.setStartArrangeId(fiveCtuRecord1.getArrangeId());
                fiveCtuCrecord.setStartLieId(fiveCtuRecord1.getLieId());
                long storageId = fiveCtuRecord1.getStorageId();
                fiveCtuCrecord.setStartStorageId(storageId);
                fiveCtuCrecord.setStatus("人工出库完成");
                fiveCtuCrecordService.insertFiveCtuCrecord(fiveCtuCrecord);

                sixWuliaoinfoService.deleteSixWuliaoinfoBywuliaocode(wuliaoCode);

                Map<String,Object> map = new HashMap<String,Object>();
                map.put("storageId",storageId);
                map.put("wuliaocode",wuliaoCode);
                fiveCtuRecordService.deleteFiveCtuRecordByStorageIdAndwuliaoInfo(map);

                String cangweiNo = fiveCtuRecord1.getStorageName();
                bindcangweiornobind(cangweiNo,0l,0l);

                String aa = gainStr(cangweiNo);
                System.out.println("仓位解绑接口返回值======="+aa);
            }
            data.put("code", 200);
            data.put("msg", "操作成功！");
        }catch(Exception e){
            e.printStackTrace();
            data.put("code", 100);
            data.put("msg", "操作失败！");
        }
        return AjaxResult.success(data);
    }
    public static String gainStr(String cangweino){
        String response = "";
        String param = "{\n" +
                "    \"reqCode\": \"\",\n" +
                "    \"reqTime\": \"\",\n" +
                "    \"clientCode\": \"\",\n" +
                "    \"tokenCode\": \"\",\n" +
                "    \"ctnrTyp\": \"1\",\n" +
                "    \"ctnrCode\": \"\",\n" +
                "    \"wbCode\": \"\",\n" +
                "    \"podCode\": \"\",\n" +
                "    \"podDir\": \"\",\n" +
                "    \"podTyp\": \"\",\n" +
                "    \"materialLot\": \"\",\n" +
                "    \"agvCode\": \"\",\n" +
                "    \"taskCode\": \"\",\n" +
                "    \"stgBinCode\": \""+cangweino+"\",\n" +
                "    \"indBind\": \"0\",\n" +
                "    \"data\": \"\"\n" +
                "}";
        JSONObject json = JSONObject.parseObject(param);
        String reqCode = UUID.randomUUID().toString().substring(0, 20);
        json.fluentPut("reqCode", reqCode);
        json.fluentPut("reqTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        json.fluentPut("taskCode", reqCode);
        response = HttpUtils.sendRestPost(rcsUrl+"bindCtnrAndBin",json);
        return response;
    }
}
